package com.budwk.app.sqfwV2.generator.baiyunqv;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGenerator.impl.SqfwTemplateExcelCommonService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelDataInto;
import com.budwk.app.sqfwV2.dto.dto.AuthenticRightDTO;
import com.budwk.app.sqfwV2.dto.dto.ContentDTO;
import com.budwk.app.sqfwV2.dto.dto.DeptDTO;
import com.budwk.app.sqfwV2.dto.image.AuthenticRightConfigBo;
import com.budwk.app.sqfwV2.enums.AuthenticRightEnum;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.policy.RenderPolicy;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.addPictureToByteList;

public class AuthenticRightExcelGenerator extends BaseExcelGenerator {

    private String templateName;

    AtomicLong atomicLong = new AtomicLong(0);


    public AuthenticRightExcelGenerator(){}

    public AuthenticRightExcelGenerator(String templateName){
        this.templateName=templateName;
    }





    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return true;
    }


    @Override
    protected void customerPopularHeaderData(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected void customerPopularHeaderDataForAdmin(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeErgodic(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected boolean addRowDataCondition(ExcelRowDataParam rowDataParam, ExcelGenerateParam excelGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularSheetData(List<Map<String, Object>> listMap, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {
        AuthenticRightDTO authenticRightDTO = (AuthenticRightDTO) rowDataParam.getAbstractCustomerPdfCreateField();
        List<DeptDTO> competentDepartment = authenticRightDTO.getCompetentDepartment();
        rowDataMap.put("num", atomicLong.incrementAndGet());
        if(ObjectUtil.isNotEmpty(authenticRightDTO.getCoordinateTime())){
            rowDataMap.put("coordinateTimeStr", DateUtil.format(authenticRightDTO.getCoordinateTime(),DatePattern.CHINESE_DATE_PATTERN));
        }
        if(ObjectUtil.isNotEmpty(authenticRightDTO.getArrivalTime())){
            rowDataMap.put("arrivalTimeStr", DateUtil.format(authenticRightDTO.getArrivalTime(),DatePattern.CHINESE_DATE_PATTERN));
        } else {
            rowDataMap.put("arrivalTimeStr", "");
        }
        if(CollectionUtil.isNotEmpty(competentDepartment)){
            List<String> deptList=competentDepartment.stream().map(DeptDTO::getName).collect(Collectors.toList());
            rowDataMap.put("deptList", String.join(",",deptList));
        } else {
            rowDataMap.put("deptList", null);
        }
        if(CollectionUtil.isNotEmpty(authenticRightDTO.getVisitPictures())){
            rowDataMap.put("visitPictures", addPictureToByteList(authenticRightDTO.getVisitPictures().get(0), 1));
        } else {
            rowDataMap.put("visitPictures", null);
        }
        if(CollectionUtil.isNotEmpty(authenticRightDTO.getHuifangVisitPictures())){
            rowDataMap.put("huifangVisitPictures", addPictureToByteList(authenticRightDTO.getHuifangVisitPictures().get(0), 1));
        } else {
            rowDataMap.put("huifangVisitPictures", null);
        }
        if(StrUtil.isNotEmpty(authenticRightDTO.getCheckedUnitLeader())){
            rowDataMap.put("checkedUnitLeader", addPictureToByteList(authenticRightDTO.getCheckedUnitLeader(), 1));
        } else {
            rowDataMap.put("checkedUnitLeader", null);
        }

    }

    @Override
    protected void customerPopularRowDataMapForAdmin(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeCreate(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected String getTemplatePath(ExcelGenerateParam excelGenerateParam) {
        return this.templateName;
    }

    @Override
    protected String getDownloadFileName(ExcelGenerateParam excelGenerateParam) {
        String code = excelGenerateParam.getExcelConfig().getFileIdentity();
        return AuthenticRightEnum.getFileName(Integer.parseInt(code));
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return AuthenticRightConfigBo.class;

    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParamBO) {

    }


}
